﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using TagThisOrgProject.Models;
using TagThisOrgProject.Models.ViewModel;
using TagThisOrgProject.Repository;
using TagThisOrgProject.Utils;
using WebMatrix.WebData;
namespace TagThisOrgProject.Controllers
{
    public class BookmarksController : ApiController
    {
        public GlobalRepository Repo { get; set; }
        public BookmarksController()
        {
            this.Repo = new GlobalRepository();
        }

        [ValidateAntiForgeryToken]
        public ApiResponse<IEnumerable<UserBookmark>> Post(UserBookmarkViewModel bkmrk)
        {
           // System.Web.Helpers.AntiForgery.
            var response = new ApiResponse<IEnumerable<UserBookmark>>();
            try
            {
                if (User.Identity.IsAuthenticated)
                    Repo.AddBookmarksForUser(User.Identity.Name, bkmrk.Url,bkmrk.Title, bkmrk.isPrivate, bkmrk.Tags.Where(t => !string.IsNullOrEmpty(t)).Select(t=>t).ToArray());
                else
                    Repo.AddBookmarksForUser(Settings.Settings.Instanse.AnonimusUserName, bkmrk.Url, bkmrk.isPrivate, bkmrk.Tags.Where(t => !string.IsNullOrEmpty(t)).Select(t=>t).ToArray());
                response.status = Status.Ok;
                response.message = "Теги успешно добавлены";
            }
            catch (Exception e)
            {
                response.status = Status.Error;
                response.message = e.Message;
            }

            return response;
        }


    }
}
